#version 460


layout(points) in;
layout(triangle_strip, max_vertices=64) out;


#include "lib/project_matrix.glsl"
#include "single_color.glsl"
#include "lib/vertex_in.glsl"


//顶点偏移和顶点数量
layout(std430, column_major, binding = 5) readonly buffer U5_Matrix {
	uvec2 m_VertOffset_Num[];
};






//layout(location = 0) in vec4 vColor[];
//layout(location = 1) in uint vIndirect[];
//layout(location = 2) in uint vInstance[];
in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	//vec4 vColor;
	//vec2 vWeights;
	//vec2 vNormal;
	//uint vIndirect;
	uint vInstance;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};

//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	/*gl_Position = mat_ProjecView * vec4(100,0,0,1);
	gColor = vec4(1,0,0,1);
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(0,0,0,1);
	gColor = vec4(0,1,0,1);
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(0,100,0,1);
	gColor = vec4(0,0,1,1);
	EmitVertex();
	EndPrimitive();
	return;*/

	mat3x2 mat = m_LP_SD[vertex_in[0].vInstance];
	uvec2 VertOffsetNum = m_VertOffset_Num[vertex_in[0].vInstance];

	//m_Vertex
	vec4 Color = f_buildColor(InstanceColor[vertex_in[0].vInstance]);

	uint vertOffset = VertOffsetNum.x;
	uint num = VertOffsetNum.y - VertOffsetNum.x;
	if(num >= 3) num -= 2;
	for(uint i=0; i<num; ++i){
		
		vec4 v = vec4(m_Vertex[VertOffsetNum.x].coord, 0, 1);
		vec2 Weights = m_Vertex[VertOffsetNum.x].weight;

		
		v.xy = (v.xy * mat[2].x) + ((mat[1]-mat[2].x) * Weights.xy);
		v.xy += mat[0];
		v.xy *= m_Scale;
		v.xy += m_Loc;

		
		gl_Position = mat_ProjecView * v;
		gl_PointSize = mat[2].x;
		gColor = Color;
		EmitVertex();

		for(uint j=1; j<3; ++j){
			v = vec4(m_Vertex[vertOffset + j].coord, 0, 1);
			Weights = m_Vertex[vertOffset + j].weight;

			v.xy = (v.xy * mat[2].x) + ((mat[1]-mat[2].x) * Weights.xy);
			v.xy += mat[0];
			v.xy *= m_Scale;
			v.xy += m_Loc;

			
			gl_Position = mat_ProjecView * v;
			gl_PointSize = mat[2].x;
			gColor = Color;
			EmitVertex();
		}
		
		EndPrimitive();
		++vertOffset;
	}
	
	

}



 